package com.fieldchain.ex.fa100.ui.kline;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringListRequestTwo;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.fieldchain.ex.fa100.utils.string.NumberFormate;
import com.github.mikephil.charting.charts.Chart;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import com.fieldchain.ex.fa100.utils.chart.auxiliary.CoupleChartGestureListener;
import com.fieldchain.ex.fa100.utils.chart.auxiliary.CoupleChartValueSelectedListener;
import com.fieldchain.ex.fa100.utils.chart.inter.MyTouchListener;
import com.fieldchain.ex.fa100.utils.chart.mk_chart.MyTimerChart;
import com.fieldchain.ex.fa100.utils.chart.mk_chart.MyVolumChart;
import com.fieldchain.ex.fa100.utils.chart.utils.data.KlineIndex;


/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/11
 */
public class TimeShareActivity extends MyBaseActivity implements View.OnClickListener, MyTouchListener.HighlightListener, IntegerCallBack {
    private TextView tvtime;
    private TextView tvOpen;
    private TextView tvHigh;
    private TextView tvLow;
    private TextView tvStatusclose;
    private TextView tvMa7;
    private TextView tvMa30;
    private MyTimerChart timeChart;
    private MyVolumChart volumChart;
    private ImageView ivBack;
    private List<List<String>> mList = new ArrayList<>();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private Timer timer;
    private String market;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    setDatas();
                    break;
            }
        }
    };
    @Override
    public void findView() {
        market=getIntent().getStringExtra("market");
        tvtime = findViewById(R.id.tvtime);
        tvOpen = findViewById(R.id.tv_open);
        tvHigh = findViewById(R.id.tv_high);
        tvLow = findViewById(R.id.tv_low);
        tvStatusclose = findViewById(R.id.tv_statusclose);
        tvMa7 = findViewById(R.id.tv_ma7);
        tvMa30 = findViewById(R.id.tv_ma30);
        timeChart = findViewById(R.id.time_chart);
        volumChart = findViewById(R.id.volum_chart);
        ivBack=findViewById(R.id.iv_back);
    }

    @Override
    public void setValue() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        initChart();
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initChart() {
        volumChart.setOnChartGestureListener(new CoupleChartGestureListener(volumChart, new Chart[]{timeChart}) {});
        timeChart.setOnChartGestureListener(new CoupleChartGestureListener(timeChart, new Chart[]{volumChart}) {});
        volumChart.setOnChartValueSelectedListener(new CoupleChartValueSelectedListener(volumChart, new Chart[]{timeChart}, this));
        timeChart.setOnChartValueSelectedListener(new CoupleChartValueSelectedListener(timeChart, new Chart[]{volumChart}, this));

        timeChart.setOnTouchListener(new MyTouchListener(timeChart, this, KlineIndex.timeChart));
        volumChart.setOnTouchListener(new MyTouchListener(volumChart, this, KlineIndex.volum));
    }

    public void setDatas() {
        HashMap map = new HashMap();
        map.put("market", market);
        map.put("type", 60 + "");
        map.put("assist", ShareManager.getInstance().getCoinType());

        HttpRequest.getInstance().getRequest(map, Constant.kline, new StringListRequestTwo() {
            @Override
            public void onSucess(List<List<String>> list, int code, String msg) {
                if (code == 200) {
                    mList.clear();
                    mList.addAll(list);
                    if (mList.size() > 0) {
                        timeChart.setValues(mList);
                        volumChart.setValues(mList);
                        updateTv(mList.size() - 1);
                    }
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_timeshare;
    }

    @Override
    public void onClick(View view) {

    }
    @Override
    public void enableHighlight() {
        timeChart.showHight();
        volumChart.showHight();
    }

    @Override
    public void disableHighlight() {
        timeChart.hidnHight();
        volumChart.hidnHight();
    }

    public void updateTv(int index) {
        List<String> data = mList.get(index);
        long time = Long.parseLong(data.get(0));
        float high = Float.parseFloat(data.get(1));
        float open = Float.parseFloat(data.get(2));
        float low = Float.parseFloat(data.get(3));
        float close = Float.parseFloat(data.get(4));
        float vol = Float.parseFloat(data.get(5));
        tvtime.setText(sdf.format(time));
        tvHigh.setText(NumberFormate.m2(high));
        tvOpen.setText(NumberFormate.m2(open));
        tvLow.setText(NumberFormate.m2(low));
        tvStatusclose.setText(NumberFormate.m2(close));

        tvMa7.setText(getMA(index, 7, mList) + "");
        tvMa30.setText(getMA(index, 30, mList) + "");
    }

    private String getMA(int index, int ma, List<List<String>> mList) {
        int count = 1;
        float sum = Float.parseFloat(mList.get(index).get(1));
        while (count < ma) {
            if (--index < 0) {
                break;
            }
            sum += Float.parseFloat(mList.get(index).get(1));
            count++;
        }
        return NumberFormate.m(sum / count+"",2,false);
    }

    @Override
    public void index(int pos) {
        updateTv(pos);
    }


    @Override
    public void onPause() {
        super.onPause();
        if (timer != null) {
            timer.cancel();
        }
        mHandler.removeCallbacksAndMessages(null);
    }

    @Override
    public void onResume() {
        super.onResume();
        startTimer();
    }

    private void startTimer() {
        if (timer != null) {
            timer = null;
        }
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        };
        timer.schedule(task, 0, 10000);
    }
}
